﻿namespace AtfutureCodeMachine
{
    using System;
    using System.Linq;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Data;
    using System.Collections;
    using System.Collections.Generic;


    using System.Windows.Forms;
    using NVelocity.App;
    using NVelocity.Runtime;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.Context;
    using System.Configuration;
    using Microsoft.VisualBasic;

    public class WebProcessing
    {
        /// <summary>
        /// 根目录
        /// </summary>
        public static string RootPath = Path.Combine(Application.StartupPath, "Web");
       
        /// <summary>
        /// 给WEB目录动态解析
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static void DoAction(string filePath, IContext context, params object[] args)
        {
            filePath = filePath.TrimEnd('/').TrimStart('/');
            string key = filePath.ToLower().Replace("/", "_").Replace(".", "_");
            
            try
            {
                typeof(WebProcessing).InvokeMember(key, System.Reflection.BindingFlags.NonPublic
                        | System.Reflection.BindingFlags.Static
                        | System.Reflection.BindingFlags.InvokeMethod
                        | System.Reflection.BindingFlags.IgnoreCase,
                        null,
                        null,
                        new object[] { context, args });
            }
            catch { }
        }

        /// <summary>
        /// 处理js/data.js页面
        /// </summary>
        /// <param name="context"></param>
        static void js_data_js(IContext context, params object[] args)
        {
            context.Put("AT_SJK", FormMain.CurrentAdo.DbInfo.Db);

            //如果是oracle，应该还要传递表名称
            DataTable dt = FormMain.CurrentAdo.GetTableInfo("");
            Dictionary<string, string> has = new Dictionary<string, string>();

            foreach (DataRow dr in dt.Rows)
            {
                has[dr["biaoen"].ToString()] = "";
            }

            StringBuilder sb = new StringBuilder();

            var keys = from k in has.Keys orderby k ascending select k;
            var i = 300;
            foreach (var key in keys)
            {
                sb.AppendLine("a.add(" + (i++) + ", 20, '" + key + "', 'table.htm?" + key + "','','','/image/jsdoc.gif');");
            }

            context.Put("AT_SJK_LIST", sb.ToString());
        }

        static void html_table_htm(IContext context, params object[] args)
        {
            //给全局变量赋值
            FormMain.g_globalinfo.TableName = FormMain.UpperFirstChar(args[0].SS());
            FormMain.g_globalinfo.ClassName = FormMain.g_globalinfo.TableName;

            context.Put("tablename", FormMain.g_globalinfo.TableName);

            //todo:这里存在问题.如果是其他数据库，不能这样取值，先考虑mssql吧。
            DataTable dt = FormMain.CurrentAdo.GetTableInfo("");

            ArrayList arr = new ArrayList();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["biaoen"].ToString().Equals(FormMain.g_globalinfo.TableName, StringComparison.OrdinalIgnoreCase) == false) continue;

                arr.Add(dr);
            }

            context.Put("list",arr);
        }

        static void html_setting_htm(IContext context, params object[] args)
        {
            string strRoot = Path.Combine(Application.StartupPath, "views");
            ArrayList arr = new ArrayList();
            foreach (DirectoryInfo di in (new DirectoryInfo(strRoot)).GetDirectories())
            {
                arr.Add("views/" + di.Name + "/");
            }

            IniFile iniFile = new IniFile();
            iniFile.Load();

            context.Put("iniFile", iniFile);

            context.Put("list", arr);
        }
    }
}